﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Collections;

public class DragableCard : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    // begin dragging

    PointerEventData beginData;
    public void OnBeginDrag(PointerEventData eventData)
    {
        
        beginData = eventData;
        print(beginData.position);
        SetDraggedPosition(eventData);
    }

    // during dragging
    public void OnDrag(PointerEventData eventData)
    {   
        SetDraggedPosition(eventData);
    }

    // end dragging
    public void OnEndDrag(PointerEventData eventData)
    {
        var defbtn = GameObject.Find("ftg_btn").GetComponent<RectTransform>();
        //print(Mathf.Abs(eventData.position[0] - (defbtn.position[0] * 200 + 360)));
       // print(eventData);
       // SetDraggedPosition(eventData);
        if (Mathf.Abs(eventData.position[0] - (defbtn.position[0] * 400 + 720)) <= 50)
        {
            SetDraggedPosition(beginData);
        }
        else
        {
            SetDraggedPosition(eventData);
        }
    }

    /// <summary>
    /// set position of the dragged game object
    /// </summary>
    /// <param name="eventData"></param>
    private void SetDraggedPosition(PointerEventData eventData)
    {
        var rt = gameObject.GetComponent<RectTransform>();

        // transform the screen point to world point int rectangle
        Vector3 globalMousePos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera, out globalMousePos))
        {   

            if (globalMousePos[1] * 100 < -15)
            { }
            else
            {
                if (globalMousePos[0] * 100 > 102 || globalMousePos[0] * 100 < -122)
                { }
                else
                {  
                    double x = -0.15;
                    globalMousePos[1] = (float)x;
                
                }
            }
            rt.position = globalMousePos;
        }
    }
}